package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户数据访问实现
 */
@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserPO getByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Long countUserByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(UserPO userPO) {
        System.out.println("保存用户信息：" + userPO.getProviderType());
        userMapper.insert(userPO);
    }

    @Override
    public UserPO getById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<UserPO> listUser(UserParam param) {
//        QueryWrapper<UserPO> queryWrapper = assmbleUserParam(param);
        List<UserPO> list = userMapper.list(param);
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

//    private QueryWrapper<UserPO> assmbleUserParam(UserParam param) {
//        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
//        if(StringUtils.isNullOrEmpty(param.getUserName())){
//            queryWrapper.like("user_name",param.getUserName());
//        }
//        if(StringUtils.isNullOrEmpty(param.getPhone())){
//            queryWrapper.like("phone",param.getPhone());
//        }
//        return queryWrapper;
//    }
}
